<?php
namespace Shama;
use Fox\Dao\SqlFragment;

use Fox\Dao\Link;

use Fox\Object\Links;
/**
 * Company Object.
 *
 * It represant a sales Company Entity.
 *
 * @author Xavier VILAIN
 * @package customer
 * @property int 	$companyid	 	Company Id
 * @property string $keyname		We store customer keyname (to check unicity faster)
 * @property string $name1			Name of the company
 * @property string $website		Company Web Site URL
 * @property string $legaluri		Link to a state web site that contain legal information
 *
 * @property string $type			Type of Company
 * @property int 	$adrid			Address Id (can be null)
 * @property bool 	$active			Is the customer active
 * @property bool 	$loekz 			Deletion Flag if true is an been deleted
 *
 * @method \Shama\Address address()	Link to the address
 */
class Company extends \Fox\Object\Versioned {

	/**
	 * Link to the address
	 */
	const LINK_ADDRESS = "address";

	/**
	 * Link to the partners
	 */
	const LINK_PARTNERS = "partners";

	/**
	 * Link to the partners
	 */
	const LINK_DOCUMENTS = "documents";

	/**
	 * Link to Type
	 */
	const LINK_TYPE = "type";

	/**
	 * Link to Status
	 */
	const LINK_STATUS = "status";

	/**
	 * Type : Leads
	 */
	const TYPE_STANDARD = "std";

	/**
	 * Change add a partner
	 */
	const CHANGE_ADD_PARTNER = "addToPartners";

	/**
	 *
	 * Status : New (company as been created)
	 * @var int
	 */
	const STATUS_NEW = '0';

	/**
	 * (non-PHPdoc)
	 * @see Fox.Object::afterSetFields($fields)
	 */
	protected function afterSetFields(array $fields)
	{
		if(isset($fields['name1']))
		{
			$this->keyname = \Fox\Utilities::cleanup($fields['name1']);
		}
		$this->address()->setFields($fields);
		return $fields;
	}

	/**
	 *
	 * Short Cut Method to find a company by name
	 * The name will be transform into keyname to make the lookup in the DB
	 * @return \Shama\Company
	 */
	public static function findByName($name)
	{
		$className = self::className();
		$finder = new $className();
		$fragments[] = new SqlFragment("keyname = ? ",array(\Fox\Utilities::cleanup($name)));
		$res = $finder->find(SqlFragment::combine($fragments));
		if(count($res) > 0)
		{
			return $res[0];
		}
		else
		{
			return $finder;
		}
	}
}

\Fox\Dao::infos(Company::className(),new \Fox\Object\Info('companies', \Fox\Databases::DB_APP));
// Link to Address Object
Links::add(\Fox\Dao::link(Company::className(),Company::LINK_ADDRESS,Link::FETCH_EAGER)
	->to(Address::className(), Address::LINK_COMPANY)
	->on('adrid')
	->oneToOne()
	->versionTo()
);
// Link to Company Partners
\Fox\Object\Links::add(\Fox\Dao::link(Company\Partner::className(),Company\Partner::LINK_COMPANY)
	->to(Company::className(),Company::LINK_PARTNERS)
	->on('companyid')
	->versionFrom());

// Link to Company Type
\Fox\Object\Links::add(\Fox\Dao::link(Company::className(),Company::LINK_TYPE)
	->to(Company\Type::className(),Company\Type::LINK_COMPANY)
	->oneToOne()
	->on('type'));

// Link to Company Status
\Fox\Object\Links::add(\Fox\Dao::link(Company::className(),Company::LINK_STATUS)
	->to(Company\Status::className(),Company\Status::LINK_COMPANY)
	->on('status')
	->oneToOne());